//
//  UIView+HDBigImageView.m
//  app
//
//  Created by apple on 2017/7/25.
//  Copyright © 2017年 hdaren. All rights reserved.
//

#import "HDBigImageView.h"

@interface HDBigImageView ()

@property (nonatomic,strong)NSString * url;

@end

@implementation  HDBigImageView

-(id)initWithParams:(NSString *)url
{
    if (self = [super init]) {
        
        self.frame = [UIApplication sharedApplication].keyWindow.screen.bounds;
        self.url = url;
        
        [self initUI];
        
    }
    
    return self;
    
}

-(void)initUI
{
    self.userInteractionEnabled = YES;
    
    transparentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth([UIScreen mainScreen].bounds), CGRectGetHeight([UIScreen mainScreen].bounds))];
    transparentView.backgroundColor = [UIColor blackColor];
    transparentView.userInteractionEnabled = YES;
    [transparentView setMultipleTouchEnabled:YES];
    transparentView.alpha = 0.0f;
    [self addSubview:transparentView];
    
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideShouFeiViewSheet)];
    tap.numberOfTapsRequired = 1;
    //    [transparentView addGestureRecognizer:tap];
    
    
//    UIButton * btn = [[UIButton alloc]initWithFrame:transparentView.frame];
//    [btn addTarget:self action:@selector(hideShouFeiViewSheet) forControlEvents:UIControlEventTouchUpInside];
    //    [transparentView addSubview:btn];
    
    
    
    CGFloat sheetWidth = UBO_SCREEN.width ;
    actionSheetHeight = UBO_SCREEN.height;
    bigImageView = [[UIView alloc] initWithFrame:CGRectOf([self.ubo fx:UB_1_2 ox:-sheetWidth/2 fy:UB_1_2 oy:-actionSheetHeight/2], [self.ubo ow:sheetWidth oh:actionSheetHeight])];
    bigImageView.backgroundColor = [UIColor whiteColor];
    bigImageView.layer.masksToBounds = YES;
    bigImageView.layer.cornerRadius = UF(10);
    bigImageView.alpha = 0.95;
    [self addSubview:bigImageView];
    
    
    __block CGRect frame = CGRectZero;
    
    frame = bigImageView.frame;
    frame.size.height = actionSheetHeight;
    bigImageView.frame = frame;
    
    
    UIImageView * iv = [[UIImageView alloc]initWithFrame:CGRectOf([bigImageView.ubo fx:0 fy:0], [bigImageView.ubo ow:UBO_SCREEN.width oh:UBO_SCREEN.height])];
    [iv sd_setImageWithURL:[NSURL URLWithString:self.url]];
    [bigImageView addSubview:iv];
    
    
    UIButton * btn = [[UIButton alloc]initWithFrame:CGRectOf([bigImageView.ubo fx:0 fy:0], [bigImageView.ubo ow:UBO_SCREEN.width oh:UBO_SCREEN.height])];
    [btn addTarget:self action:@selector(hideBigImageViewSheet) forControlEvents:UIControlEventTouchUpInside];
    [bigImageView addSubview:btn];
    
    
}

- (void)cancleEvent:(id)sender
{
    
    
    
    [self hideBigImageViewSheet];
}


#pragma mark -
#pragma mark PickerView show/hide

- (void)hideBigImageViewSheet
{
    
    NSLog(@"11111");
    
    
    //    [self.timer invalidate];
    //    self.timer = nil;
    //    [self.send_code_btn setTitle:@"获取验证码" forState:UIControlStateNormal];
    //    self.send_code_btn.enabled = YES;
    //    i = 1;
    
    [UIView animateWithDuration:0.2
                          delay:0.0
                        options:UIViewAnimationOptionCurveEaseOut
                     animations:^() {
                         transparentView.alpha = 0.0;
                         
                         //                         self.center = CGPointMake(CGRectGetWidth(self.frame) / 2.0, CGRectGetHeight([UIScreen mainScreen].bounds) + CGRectGetHeight(self.frame) / 2.0);
                         
                         //self.frame = CGRectMake(0, KDeviceHeight, kDeviceWidth, KDeviceHeight - 64);
                         
                         [transparentView removeFromSuperview];
                         [self removeSelf];
                         
                     } completion:^(BOOL finished) {
                         
                         
                         
                     }];
}

- (void)removeSelf
{
    [self removeFromSuperview];
    
    //    self.isShow = NO;
    
}

- (void)showBigImageViewSheet
{
    [self showBigImageViewInView:kWindow];
}

- (void)showBigImageViewInView:(UIView *)view
{
    //    CGFloat vH = CGRectGetHeight(view.frame);
    
    self.frame = CGRectMake(0, 0, UBO_SCREEN.width, UBO_SCREEN.height);
    //    actionSheetY = vH - actionSheetHeight;
    
    [view addSubview:self];
    //    [view insertSubview:transparentView belowSubview:self];
    
    //    self.isShow = YES;
    
    [UIView animateWithDuration:0.2
                          delay:0.0
                        options:UIViewAnimationOptionCurveEaseOut
                     animations:^() {
                         transparentView.alpha = 0.4;
                         //                         self.frame = CGRectMake(0, actionSheetY, kDeviceWidth, actionSheetHeight);
                     }
                     completion:^(BOOL finished) {}];
}


@end
